//
//  ChargeSyncController.m
//  Stock
//
//  Created by black sun on 12-3-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ChargeSyncController.h"
#import "StockAppHelper.h"
#import "UIView+LoadingView.h"

@implementation ChargeSyncController
@synthesize loginView;
@synthesize signupView;
@synthesize forgotPasswdView;
@synthesize didLoginView;
@synthesize loginUserName;
@synthesize loginPasswd;
@synthesize sinnupUserName;
@synthesize signupPasswd;
@synthesize signupPasswdConfirm;
@synthesize forgetUserName;
@synthesize forgetPasswd;
@synthesize forgetPasswdConfirm;
@synthesize usernameLabel;
@synthesize chargeSyncService;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"同步购买";
    self.chargeSyncService = [[[StockChargeSyncService alloc] initWithDelegate:self] autorelease];
    if ([[StockContext shareInstance] savedChargeEmail].length != 0) {
        self.usernameLabel.text = [[StockContext shareInstance] savedChargeEmail];
        [self.view addSubview:self.didLoginView];
    }
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(IBAction)hideKeyBoard:(id)sender{
    [self.loginUserName resignFirstResponder];
    [self.loginPasswd resignFirstResponder];
    [self.sinnupUserName resignFirstResponder];
    [self.signupPasswd resignFirstResponder];
    [self.signupPasswdConfirm resignFirstResponder];
    [self.forgetUserName resignFirstResponder];
    [self.forgetPasswd resignFirstResponder];
    [self.forgetPasswdConfirm resignFirstResponder];
}
-(IBAction)login:(id)sender{
    if (self.loginUserName.text.length==0) {
        [[StockAppHelper shareInstance] showAlertViewWithMessage:@"请输入邮箱"];
        return;
    }else if (self.loginPasswd.text.length==0) {
        [[StockAppHelper shareInstance] showAlertViewWithMessage:@"请输入密码"];
        return;
    }
    [self.view showHUDLoadingView:YES];
    [self.chargeSyncService loginAndSync:self.loginUserName.text passwd:self.loginPasswd.text];
}
-(IBAction)signup:(id)sender{
    if (self.sinnupUserName.text.length==0) {
        [[StockAppHelper shareInstance] showAlertViewWithMessage:@"请输入邮箱"];
        return;
    }else if (self.signupPasswd.text.length==0 || self.signupPasswdConfirm.text.length==0) {
        [[StockAppHelper shareInstance] showAlertViewWithMessage:@"请输入密码"];
        return;
    }else if (![self.signupPasswd.text isEqualToString:self.signupPasswdConfirm.text]) {
        [[StockAppHelper shareInstance] showAlertViewWithMessage:@"两次输入的密码必须一致"];
        return;
    }
    [self.view showHUDLoadingView:YES];
    [self.chargeSyncService signupUser:self.sinnupUserName.text passwd:self.signupPasswd.text];
}
-(IBAction)resetPassword:(id)sender{
    if (self.forgetUserName.text.length==0) {
        [[StockAppHelper shareInstance] showAlertViewWithMessage:@"请输入邮箱"];
        return;
    }else if (self.forgetPasswd.text.length==0 || self.forgetPasswdConfirm.text.length==0) {
        [[StockAppHelper shareInstance] showAlertViewWithMessage:@"请输入密码"];
        return;
    }else if (![self.forgetPasswd.text isEqualToString:self.forgetPasswdConfirm.text]) {
        [[StockAppHelper shareInstance] showAlertViewWithMessage:@"两次输入的密码必须一致"];
        return;
    }
    [self.view showHUDLoadingView:YES];
    [self.chargeSyncService modifyPasswd:self.forgetUserName.text passwd:self.forgetPasswd.text];
}

-(IBAction)switchToSingup:(id)sender{
    [UIView beginAnimations:@"switchToSingup" context:nil];
    [UIView setAnimationDuration:0.7];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:NO];
    [self.view addSubview:self.signupView];
    //[self.view insertSubview:self.signupView belowSubview:self.loginView];
    [UIView commitAnimations];
}
-(IBAction)switchToForgetPasswd:(id)sender{
    [UIView beginAnimations:@"switchToForgetPasswd" context:nil];
    [UIView setAnimationDuration:0.7];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:NO];
    [self.view addSubview:self.forgotPasswdView];
    //[self.view insertSubview:self.forgotPasswdView belowSubview:self.loginView];
    [UIView commitAnimations];
}

-(void)viewDidUnload{
    [super viewDidUnload];
    self.loginView=nil;
    self.signupView=nil;
    self.forgotPasswdView=nil;
    self.didLoginView = nil;
    self.loginUserName=nil;
    self.loginPasswd=nil;
    self.sinnupUserName=nil;
    self.signupPasswd=nil;
    self.signupPasswdConfirm=nil;
    self.forgetUserName=nil;
    self.forgetPasswd=nil;
    self.forgetPasswdConfirm=nil;
    self.chargeSyncService = nil;
    self.usernameLabel = nil;
}
-(void)dealloc{
    self.loginView=nil;
    self.signupView=nil;
    self.forgotPasswdView=nil;
    self.didLoginView = nil;
    self.loginUserName=nil;
    self.loginPasswd=nil;
    self.sinnupUserName=nil;
    self.signupPasswd=nil;
    self.signupPasswdConfirm=nil;
    self.forgetUserName=nil;
    self.forgetPasswd=nil;
    self.forgetPasswdConfirm=nil;
    self.chargeSyncService = nil;
    self.usernameLabel = nil;
    [super dealloc];
}

-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    [self.view showHUDLoadingView:NO];
    NSString *email = [aUserinfo objectForKey:ChargeUserNameKey];
    self.usernameLabel.text = email;
    [UIView beginAnimations:@"didLogin" context:nil];
    [UIView setAnimationDuration:0.7];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
    [self.view addSubview:self.didLoginView];
    [UIView commitAnimations];
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    [self.view showHUDLoadingView:NO];
    [[StockAppHelper shareInstance] showAlertViewWithMessage:aError.domain];
}

@end
